//
//  StaffLogic.m
//  QRMaster
//
//  Created by myApple on 15/12/21.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "GuestLogic.h"
#import "OperationDataBaseModel.h"

@implementation GuestLogic


-(void)insertStaffData:(GuestModel *)model withCommuntiyId:(NSString *)communityId
{
    NSMutableDictionary * guestDict = [self modelConvertDict:model];
    [guestDict setValue:communityId forKey:@"communityId"];
    OperationDataBaseModel * operation = [[OperationDataBaseModel alloc]init];
    [operation insertDataTable:@"customers" withDict:guestDict];
}

/**
 *  模型转化为字典
 *
 *  @param model 客户模型
 *
 *  @return 可变字典
 */
-(NSMutableDictionary *)modelConvertDict:(GuestModel *)model
{
    NSMutableDictionary * staffDict = [[NSMutableDictionary alloc]init];
    [staffDict setValue:model.guestid forKey:@"id"];
    [staffDict setValue:model.guestName forKey:@"name"];
    [staffDict setValue:model.sex forKey:@"gender"];
    [staffDict setValue:model.headUrl forKey:@"headUrl"];
    [staffDict setValue:model.telePhone forKey:@"mobile"];
    [staffDict setValue:model.certificaties forKey:@"identityId"];
    [staffDict setValue:model.firstChar forKey:@"firstChar"];
    [staffDict setValue:model.email forKey:@"email"];
    [staffDict setValue:model.areaCode forKey:@"areaCode"];
    return staffDict;
}

/**
 *  查询客户信息
 *
 *  @param communityId 集群ID
 *
 *  @param block       集群用于接收查询到的数组
 */
-(void)queryGuestInfo:(NSString *)communityId block:(arrBlock)arrBlock
{
    NSMutableArray *guestArray=[[NSMutableArray alloc]init];
    OperationDataBaseModel * operation = [[OperationDataBaseModel alloc]init];
    [operation queryDataFromTable:@"customers" withCondition:[NSString stringWithFormat:@"where communityId = %@",communityId] block:^(FMResultSet *set) {
        while ([set next]) {
            GuestModel *guestModel=[[GuestModel alloc]init];
            guestModel.guestid=[set stringForColumn:@"id"];
            guestModel.guestName=[set stringForColumn:@"name"];
            guestModel.telePhone=[set stringForColumn:@"mobile"];
            guestModel.sex=[set stringForColumn:@"gender"];
            guestModel.firstChar=[set stringForColumn:@"firstChar"];
            guestModel.email=[set stringForColumn:@"email"];
            guestModel.certificaties=[set stringForColumn:@"identityId"];
            guestModel.headUrl=[set stringForColumn:@"headUrl"];
            guestModel.areaCode = [set stringForColumn:@"areaCode"];
            [guestArray addObject:guestModel];
        }
        if (arrBlock) {
            arrBlock(guestArray);
        }
    }];
}

/**
 *
 *  @param communityId 集群ID
 *
 *  @param guestId     客户ID
 */
-(void)deleteGuestInfo:(NSString *)guestId withCommunityId:(NSString *)communityId
{
    OperationDataBaseModel * operation = [[OperationDataBaseModel alloc]init];
    [operation deleteDataFromTable:@"customers" withCondition:[NSString stringWithFormat:@"where id in (%@) and communityId = %@",guestId,communityId]];
}

/**
 *
 *  @param communityId 集群ID
 *
 *  @param guestId     客户ID
 *
 *  @param dic         客户需要更新的键值对
 */
-(void)updateGuestInfo:(NSString *)guestId communityId:(NSString *)communityId dic:(NSMutableDictionary *)dic
{
    OperationDataBaseModel * operation = [[OperationDataBaseModel alloc]init];
    [operation updateDataFromTable:@"customers" withCondition:[NSString stringWithFormat:@"where id = %@ and communityId = %@",guestId,communityId] withDict:dic];
}

/**
 *  根据集群id删除该集群下客户信息
 *
 *  @param communityId 集群ID
 *
 */
-(void)deleteGuestInfo:(NSString *)communityId
{
    OperationDataBaseModel * operation = [[OperationDataBaseModel alloc]init];
    [operation deleteDataFromTableByCommunityId:@"customers" communityId:communityId];
}

@end
